package com.lancer.backlog.service;

import com.lancer.backlog.beans.UserInfo;
import com.lancer.backlog.common.bean.ResultBean;
import com.lancer.backlog.common.bean.StatusBean;
import com.lancer.backlog.common.exception.ParamException;
import com.lancer.backlog.dao.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName: UserService
 * @Description: TODO
 * @Auther: lcDai
 * @Date: 2020/8/17 14:09
 * @UpdateUser:
 * @UpdateDate:
 * @Version: 1.0
 **/
@Service
public class  UserService {
    private final Logger log = LoggerFactory.getLogger(UserService.class);


    @Autowired
    private UserMapper userMapper;


    /**
     * 获取用户列表
     * @return
     */
    public List<UserInfo> getUserList(){
        return userMapper.getUserList();
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    public UserInfo getUserInfo(Integer id) {
        return userMapper.getUserInfo(id);
    }

    /**
     * 根据账号获取用户信息
     * @param username
     * @return
     */
    public UserInfo getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    /**
     * 注册账号
     * @param userInfo
     * @return
     */
    public void addUser(UserInfo userInfo) {
        String username=userInfo.getUsername();
        //判断是否账号重复
        UserInfo user = userMapper.getUserByUsername(username);
        if(user==null){
            log.info("添加用户:{}",username);
            // 默认用户状态为审核通过
            user.setState(3);
            userMapper.addUser(userInfo);
        }else {
            log.info("账号:{}已存在",username);
            throw new ParamException("账号已存在");
        }
    }


    public void updateUser(UserInfo userInfo) {
        userMapper.updateUser(userInfo);
    }
}